#include "Transformation.h"

int Transformation::read(TiXmlElement* transformation)
{
	float v1,v2,v3;
	string tag_view,axis;
	GLdouble current[16];
	TiXmlElement *res=transformation->FirstChildElement();

	glLoadIdentity();
	glGetDoublev(GL_MODELVIEW_MATRIX, current);
	for(TiXmlElement *res(transformation->FirstChildElement()); res != NULL; res = res->NextSiblingElement())
	{
		tag_view=res->Value();
		glLoadIdentity();
		if(tag_view=="translate")
		{
			if (res->QueryFloatAttribute("x",&v1)==TIXML_SUCCESS&&
				res->QueryFloatAttribute("y",&v2)==TIXML_SUCCESS&&
				res->QueryFloatAttribute("z",&v3)==TIXML_SUCCESS)
			{
				glMultMatrixd(current);
				glTranslatef(v1,v2,v3);
				
				glGetDoublev(GL_MODELVIEW_MATRIX, current);
				cout<<"TRANSLATE: "<<v1<<" "<<v2<<" "<<v3<<endl;
			}
		}
		else if(tag_view=="scale")
		{
			if (res->QueryFloatAttribute("x",&v1)==TIXML_SUCCESS&&
				res->QueryFloatAttribute("y",&v2)==TIXML_SUCCESS&&
				res->QueryFloatAttribute("z",&v3)==TIXML_SUCCESS)
			{
				glMultMatrixd(current);
				glScalef(v1,v2,v3);
				
				glGetDoublev(GL_MODELVIEW_MATRIX, current);
				cout<<"SCALE: "<<v1<<" "<<v2<<" "<<v3<<endl;
			}
		}
		else if(tag_view=="rotate")
		{
			axis.empty();
			axis = res->Attribute("axis");
			if (axis!=""&&
				res->QueryFloatAttribute("angle",&v2)==TIXML_SUCCESS)
			{
				glMultMatrixd(current);
				if(axis=="x")glRotatef(v2,1,0,0);
				else if(axis=="y")glRotatef(v2,0,1,0);
				else if(axis=="z")glRotatef(v2,0,0,1);
				
				glGetDoublev(GL_MODELVIEW_MATRIX, current);
				
				cout<<"ROTATE: "<<axis<<" "<<v2<<endl;
			}
		}
		
		//if(ashininess->QueryFloatAttribute("value",&shininess_value)==TIXML_SUCCESS)
	}
	glLoadIdentity();
	glMultMatrixd(current);
	glGetDoublev(GL_MODELVIEW_MATRIX, matrix);
	glLoadIdentity();
	return 1;
	
}